package com.inoneone.youba.mcht.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.inoneone.toolkit.enums.MchActived;
import com.inoneone.youba.mcht.dao.mapper.*;
import com.inoneone.youba.mcht.dao.po.*;
import com.inoneone.youba.mcht.dto.req.*;
import com.inoneone.youba.mcht.dto.rsp.AddOilRsp;
import com.inoneone.youba.mcht.dto.rsp.MyTankerRsp;
import com.inoneone.youba.mcht.dto.rsp.SystemInfo;
import com.inoneone.youba.mcht.enums.ErrorCode;
import com.inoneone.toolkit.enums.MchtStatus;
import com.inoneone.toolkit.exception.BusinessRuntimeException;
import com.inoneone.youba.mcht.service.MerchantApi;
import com.inoneone.youba.mcht.util.CommonUtil;
import com.inoneone.youba.mcht.util.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class MerchantApiImpl implements MerchantApi {
	@Autowired
	private MerchantMapper merchantMapper;

	@Autowired
	private ExtendTankerMapper extendTankerMapper;

	@Autowired
	private ExtendCardMapper extendCardMapper;

	@Autowired
	private TankerMapper tankerMapper;

	@Autowired
	private ExtendMerchantMapper extendMerchantMapper;


	/**
	 * Description: 激活商户</br>
	 * Date: 2017-07-20 11:56:55</br>
	 *
	 * @param
	 * @return
	 * @author 姬远玄
	 * @since JDK 1.7
	*/
	@Override
	public void activeMch(String mchId, String enPwd, String enPayPwd)
	{
		Merchant merchant = new Merchant();
		merchant.setActived(MchActived.ACTIVED.getCode());
		merchant.setId(mchId);
		merchant.setPassword(enPwd);
		merchant.setPayPassword(enPayPwd);
		merchantMapper.updateByPrimaryKeySelective(merchant);
	}

	/**
	 * Description: 绑定openId</br>
	 * Date: 2017-07-20 18:52:31</br>
	 *
	 * @param
	 * @return
	 * @author 姬远玄
	 * @since JDK 1.7
	*/
	@Override
	public void bindOpenId(String mchId, String openId)
	{
		Merchant merchant = new Merchant();
		merchant.setId(mchId);
		merchant.setOpenId(openId);
		extendMerchantMapper.updateByPrimaryKey(merchant);
	}

	/**
	 * Description: 查询商户的系统信息</br>
	 * Date: 2017-06-20 04:14:00</br>
	 *
	 * @param mchId@return
	 * @author 姬远玄
	 * @since JDK 1.7
	 */
	@Override
	public SystemInfo querySystemInfo(String mchId) {

		//查询加油机数据
		TankerExample tankerExample = new TankerExample();
		tankerExample.createCriteria().andMchIdEqualTo(mchId);
		SysInfo sysInfo = extendTankerMapper.countSysInfo(tankerExample);

		//查询ic卡数据
		CardExample cardExample = new CardExample();
		cardExample.createCriteria().andMchIdEqualTo(mchId);
		CardInfo cardInfo = extendCardMapper.countCardInfo(cardExample);

		//封装返回数据
		SystemInfo systemInfo = new SystemInfo();
		BeanUtils.copyProperties(sysInfo, systemInfo);
		BeanUtils.copyProperties(cardInfo, systemInfo);
		systemInfo.setTotalCard(cardInfo.getTotal());
		systemInfo.setTotalTanker(sysInfo.getTotal());
		return systemInfo;
	}

	/**
	 * Description: 查询我的加油机信息</br>
	 * Date: 2017-06-21 11:28:07</br>
	 *
	 * @param
	 * @return
	 * @author 姬远玄
	 * @since JDK 1.7
	*/
	@Override
	public List<Tanker> queryTankerInfo(MyTankerReq myTankerReq, String mchId)
	{
		TankerExample tankerExample = new TankerExample();
		TankerExample.Criteria criteria = tankerExample.createCriteria().andMchIdEqualTo(mchId);
//		if(!CommonUtil.isNullOrEmpty(myTankerReq.getName()))
//		{
//			criteria.andNameLike(myTankerReq.getName() + "%");
//		}
		List<Tanker> tankers = tankerMapper.selectByExample(tankerExample);
		MyTankerRsp myTankerRsp = new MyTankerRsp();
		return tankers;
	}

	/**
	 * Description: 加油机增加油量</br>
	 * Date: 2017-06-21 14:38:09</br>
	 *
	 * @param addOilReq @return
	 * @author 姬远玄
	 * @since JDK 1.7
	 */
	@Override
	public AddOilRsp addOil(AddOilReq addOilReq, String mchId) {

		//增加油量数据
		Tanker tanker = new Tanker();
		tanker.setTankerId(addOilReq.getTankerId());
		tanker.setRemainFuel(new BigDecimal(addOilReq.getOil()));
		int result = extendTankerMapper.addFuel(tanker);
		if (result != 1)
		{
			throw  new BusinessRuntimeException(ErrorCode.ERR_UPDATE);
		}

		//返回结果
		Tanker tankerResult = tankerMapper.selectByPrimaryKey(addOilReq.getTankerId());
		AddOilRsp addOilRsp = new AddOilRsp();
		addOilRsp.setCurrentOil(tankerResult.getRemainFuel());
		addOilRsp.setTankerId(tankerResult.getTankerId());
		return addOilRsp;
	}

	/**
	 * Description: 编辑加油机信息</br>
	 * Date: 2017-06-21 16:25:35</br>
	 *
	 * @param editTankerReq@return
	 * @author 姬远玄
	 * @since JDK 1.7
	 */
	@Override
	public void editTanker(EditTankerReq editTankerReq) {
		Tanker tanker = new Tanker();
		BeanUtils.copyProperties(editTankerReq, tanker);
		tanker.setTankerId(editTankerReq.getTankerId());


		int result = tankerMapper.updateByPrimaryKeySelective(tanker);
		if (result != 1)
		{
			throw  new BusinessRuntimeException(ErrorCode.ERR_UPDATE);
		}
	}

	@Override
	public void editPrice(EditPriceReq editPriceReq) {
		Tanker tanker = new Tanker();
		tanker.setPrice(new BigDecimal(editPriceReq.getPrice()));
		tanker.setTankerId(editPriceReq.getTankerId());

		int result = tankerMapper.updateByPrimaryKeySelective(tanker);
		if (result != 1)
		{
			throw  new BusinessRuntimeException(ErrorCode.ERR_UPDATE);
		}
	}

	/**
	 * Description: 编辑加油机信息</br>
	 * Date: 2017-06-21 16:25:35</br>
	 *
	 * @param addOilReq
	 * @return
	 * @author 姬远玄
	 * @since JDK 1.7
	 */
	@Override
	public AddOilRsp adjustOil(AddOilReq addOilReq) {
		Tanker tanker = new Tanker();
		tanker.setRemainFuel(new BigDecimal(addOilReq.getOil()));
		tanker.setTankerId(addOilReq.getTankerId());


		int result = tankerMapper.updateByPrimaryKeySelective(tanker);
		if (result != 1)
		{
			throw  new BusinessRuntimeException(ErrorCode.ERR_UPDATE);
		}
		AddOilRsp addOilRsp = new AddOilRsp();
		addOilRsp.setTankerId(addOilReq.getTankerId());
		addOilRsp.setCurrentOil(new BigDecimal(addOilReq.getOil()));
		return  addOilRsp;
	}

	/**
	 * Description: 编辑我的信息</br>
	 * Date: 2017-06-22 09:48:59</br>
	 *
	 * @param editMyinfoReq
	 * @return
	 * @author 姬远玄
	 * @since JDK 1.7
	 */
	@Override
	public void editMyinfo(EditMyInfoReq editMyinfoReq, String mchId) {
		Merchant merchant = new Merchant();
		merchant.setId(mchId);
		BeanUtils.copyProperties(editMyinfoReq, merchant);
		int result = merchantMapper.updateByPrimaryKeySelective(merchant);
		if(result != 1)
		{
			throw  new BusinessRuntimeException(ErrorCode.ERR_UPDATE);
		}
	}

	/**
	 * Description: 查询加油机</br>
	 * Date: 2017-06-22 15:28:30</br>
	 *
	 * @param
	 * @return
	 * @author 姬远玄
	 * @since JDK 1.7
	*/
	@Override
	public Tanker queryTanker(String tankerId)
	{
		Tanker tanker = tankerMapper.selectByPrimaryKey(tankerId);

		if(tanker == null)
		{
			throw new BusinessRuntimeException(ErrorCode.TANKER_NOT_EXIST);
		}
		return tanker;
	}

	/**
	 * Description: 查询一个商户信息</br>
	 * Date: 2017-06-29 14:56:42</br>
	 *
	 * @param
	 * @return
	 * @author 姬远玄
	 * @since JDK 1.7
	*/
	@Override
	public Merchant queryMch(String mobile)
	{
		MerchantExample merchantExample = new MerchantExample();
		merchantExample.createCriteria().andMobileEqualTo(mobile);
		List<Merchant> merchants =merchantMapper.selectByExample(merchantExample);
		if (merchants.size() != 1)
		{
			throw new BusinessRuntimeException(ErrorCode.MCH_NOT_EXIST);
		}

		return merchants.get(0);
	}

	/**
	 * Description: 根据openid查询商户信息</br>
	 * Date: 2017-07-20 18:44:43</br>
	 *
	 * @param
	 * @return
	 * @author 姬远玄
	 * @since JDK 1.7
	*/
	@Override
	public Merchant queryMchByOpenid(String openId)
	{
		MerchantExample merchantExample = new MerchantExample();
		merchantExample.createCriteria().andOpenIdEqualTo(openId);
		List<Merchant> merchants =merchantMapper.selectByExample(merchantExample);
		if (merchants.size() != 1)
		{
			return null;
		}

		return merchants.get(0);
	}

	/**
	 * Description: 查询商户信息</br>
	 * Date: 2017-07-19 18:51:00</br>
	 *
	 * @param
	 * @return
	 * @author 姬远玄
	 * @since JDK 1.7
	*/
	@Override
	public Merchant queryMchById(String mchId)
	{
		Merchant merchant = merchantMapper.selectByPrimaryKey(mchId);
		if(merchant == null)
		{
			throw new BusinessRuntimeException(ErrorCode.MCH_NOT_EXIST);
		}
		return merchant;
	}
}
